package br.com.californiahotel.client.viewControler.locacao;

import java.util.List;

import br.com.californiahotel.client.CaliforniaHotel;
import br.com.californiahotel.client.CaliforniaHotel.History;
import br.com.californiahotel.client.architecture.viewController.MaintenanceController;
import br.com.californiahotel.client.interfaces.AsyncCallback;
import br.com.californiahotel.client.service.LocacaoService;
import br.com.californiahotel.client.service.LocacaoServiceAsync;
import br.com.californiahotel.shared.models.Locacao;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class LocacaoController extends MaintenanceController {

	private LocacaoServiceAsync locacaoService;
	private final Display disp;

	public interface Display extends MaintenanceDisplay {
		ToolStripButton getDespesasButton();
	}

	public LocacaoController(Display display) {
		super(display);
		disp = display;
	}

	@Override
	protected void setGridContent() {
		locacaoService = GWT.create(LocacaoService.class);
		locacaoService.findAll(new AsyncCallback<List<Locacao>>() {

			@Override
			public void onSuccess(List<Locacao> result) {
				if (result.size() == 0) {
					getMaintenanceDisplay().getGrid().setData(
							new ListGridRecord[] {});
				} else {
					ListGridRecord[] listGridRecord = new ListGridRecord[result
					                                                     .size()];
					for (int i = 0; i < result.size(); i++) {
						listGridRecord[i] = new ListGridRecord();
						setAttributes(listGridRecord[i], result.get(i));
					}
					getMaintenanceDisplay().getGrid().setData(listGridRecord);
				}
			}

			private void setAttributes(ListGridRecord listGridRecord,
					Locacao locacao) {
				listGridRecord.setAttribute("id", locacao.getId());
				listGridRecord.setAttribute("hospede", locacao.getHospede());
				listGridRecord.setAttribute("apto", locacao.getApartamento());
			}
		});
	}

	@Override
	protected void bind() {
		getMaintenanceDisplay().getNewButton().addClickHandler(newButtonClickEvent(History.NOVA_LOCACAO));
		getMaintenanceDisplay().getDeleteButton().addClickHandler(deleteButtonClickEvent());
		getMaintenanceDisplay().getEditButton().addClickHandler(editButtonClickEvent());
		disp.getDespesasButton().addClickHandler(despesaButtonClickEvent());

	}

	private ClickHandler despesaButtonClickEvent() {
		return new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				CaliforniaHotel.changeMainContent(new DespesaController(new DespesaView(getMaintenanceDisplay().getGrid().getSelectedRecord().getAttribute("id"))).getView());
			}

		};
	}

	private ClickHandler editButtonClickEvent() {
		return new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

			}

		};
	}

	private ClickHandler deleteButtonClickEvent() {
		return new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ListGridRecord selectedRecord = getMaintenanceDisplay()
						.getGrid().getSelectedRecord();
				if (selectedRecord != null) {
					locacaoService.delete(
							selectedRecord.getAttribute("id"),
							new AsyncCallback<Boolean>() {

								@Override
								public void onSuccess(Boolean result) {
									if (result == true) {
										setGridContent();
									} else {
										SC.warn("Erro ao excluir locacao");
									}
								}
							});
				}
			}
		};
	}

}
